import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import drive.DriveLocations;

public class VdoMain {

	private String[] fileList;
	private static String OUTPUT_DIR
		= "C:\\Temp\\Vdo";
	private File aimDir = null;

	public static void main(String[] args) throws Throwable {
		new VdoMain().run(DriveLocations.BONNIE + "\\Sport\\100328_NBA_Kings_at_Cavaliers\\vdo\\top");
		new VdoMain().run(DriveLocations.BONNIE + "\\Sport\\100328_NBA_Kings_at_Cavaliers\\vdo\\old");
		new VdoMain().run(DriveLocations.BONNIE + "\\Sport\\100328_NBA_Kings_at_Cavaliers\\vdo\\new");
		openOutDir();
	}

	private void run(String anAimDir) throws Throwable {
		aimDir = new File(anAimDir);
		createFileList();
		createBatFiles();
		System.out.println("Done.");
	}

	private static void openOutDir() {
		try {
			Runtime.getRuntime().exec("explorer \"" + OUTPUT_DIR + "\"");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void createBatFiles() throws Throwable {
		new File(OUTPUT_DIR).mkdirs();
		System.out.println(OUTPUT_DIR + "\\app_and_go_to_"
			+ aimDir.getName() + ".bat");
		File appBat = new File(OUTPUT_DIR, "app_and_go_to_"
			+ aimDir.getName() + ".bat");
		appBat.createNewFile();
		
		BufferedWriter bw = new BufferedWriter(
				new FileWriter(appBat));
		bw.append(DriveLocations.BONNIE + "\r\ncd \"").append(aimDir.getAbsolutePath())
		  .append("\"\r\n");
		
		for (int i = 0; i < fileList.length; i++) {
			bw.append("attrib -h \"").append(fileList[i])
			  .append("\"\r\n");
		}
		
		bw.append("explorer \"").append(aimDir.getAbsolutePath())
		  .append("\"").close();

		File disBat = new File(OUTPUT_DIR, "dis_and_go_to_"
			+ aimDir.getName() + ".bat");
		bw = new BufferedWriter(
				new FileWriter(disBat));
		bw.append(DriveLocations.BONNIE + "\r\ncd \"").append(aimDir.getAbsolutePath())
		  .append("\"\r\n");
		
		for (int i = 0; i < fileList.length; i++) {
			bw.append("attrib +h \"").append(fileList[i])
			  .append("\"\r\n");
		}
		
		bw.append("explorer \"").append(aimDir.getAbsolutePath())
		  .append("\"").close();
	}

	private void createFileList() throws Throwable {
		File[] files = aimDir.listFiles();
		
		for (int i = 0; i < files.length; i++) {
			if (isMovieFile(files[i])) {
				String name = files[i].getName();
				int pointLoc = name.indexOf(".");
				files[i].renameTo(new File(aimDir, name.substring(0, pointLoc) + ".vdo"));
			}
		}
		
		fileList = aimDir.list();
	}

	private boolean isMovieFile(File file) {
		String name = file.getName();
		return name.endsWith(".mp4") || name.endsWith(".wmv")
			|| name.endsWith(".avi") || name.endsWith(".flv");
	}
}
